﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///  模組: Department Module
///  作者: Adams <adamschao@gmail.com>
///  修改: 2013-3-25
///  目的: 取得使用者是屬於集團中哪個層級共用方法
///  參數: 使用者編號
/// </summary>
public class BLogical
{
    /// <summary>
    /// 取得使用者是屬於集團中哪個層級
    /// </summary>
    /// <param name="userID">欲查詢之使用者員編</param>
    /// <returns>
    /// 0:沒找到對應部門 1:公司級 2:營級 3:處級 4:區級 5:所級
    /// </returns>
    public static int GetDeptLevelbyUser(string userID)
    {
        string deptID = string.Empty;
        int deptLevel = 0;
        MISEntities mis = new MISEntities();
        var user = mis.MIS_USER_AP.Where(u => u.USER_ID == userID).SingleOrDefault();
        if (user != null) deptID = user.DEPT_ID;

        //載入使用者清單
        using (AS400Entities as400 = new AS400Entities())
        {
            //列出公司-1
            var depts = from b in as400.AS400_BIF41
                        join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                        where b.BI4101 == b.BI4119 && b.BI4101 == deptID
                        select new { b.BI4101, z.F30603 };
            if (depts.Count() > 0)
            {
                deptLevel = 1;
            }
            else
            {  //列出營級-2
                var depts2 = from b in as400.AS400_BIF41
                             join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                             where b.BI4101 == b.BI4115 && b.BI4101 == deptID
                             select new { b.BI4101, z.F30603, b.BI4119 };
                if (depts2.Count() > 0)
                {
                    deptLevel = 2;
                }
                else
                {
                    //列出處級-3
                    var depts3 = from b in as400.AS400_BIF41
                                 join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                                 where b.BI4101 == b.BI4114 && b.BI4101 == deptID
                                 select new { b.BI4101, z.F30603, b.BI4115 };
                    if (depts3.Count() > 0)
                    {
                        deptLevel = 3;
                    }
                    else
                    {
                        //列出區級-4
                        var depts4 = from b in as400.AS400_BIF41
                                     join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                                     where b.BI4101 == b.BI4113 && b.BI4101 == deptID
                                     select new { b.BI4101, z.F30603, b.BI4114 };
                        if (depts4.Count() > 0)
                        {
                            deptLevel = 4;
                        }
                        else
                        {
                            //列出所級-5
                            var depts5 = from b in as400.AS400_BIF41
                                         join z in as400.AS400_ZDF306 on b.BI4101 equals z.F30600
                                         where b.BI4101 == b.BI4112 && b.BI4101 == deptID
                                         select new { b.BI4101, z.F30603, b.BI4113 };
                            if (depts5.Count() > 0)
                            {
                                deptLevel = 5;
                            }
                        }
                    }
                }
            }
        }
        return deptLevel;
    }

    /// <summary>
    /// 由部門編號取得部門名稱
    /// </summary>
    /// <param name="deptID">部門編號</param>
    /// <returns>部門名稱</returns>
    public static string GetDeptNameByID(string deptID)
    {
        string deptName = string.Empty;
        using (MISEntities mis = new MISEntities())
        {
            var dept = mis.MIS_DEPT_AP.Where(d => d.DEPT_ID == deptID).SingleOrDefault();
            if (dept!=null)
            {
                deptName = dept.DEPT_NAME;                 
            }
        }
        return deptName;
    }

    /// <summary>
    /// 由使用者編號取得使用者名稱
    /// </summary>
    /// <param name="userID">員工編號</param>
    /// <returns>員工名稱</returns>
    public static string GetUserNameByID(string userID)
    {
        string userName = string.Empty;
        using (MISEntities mis = new MISEntities())
        {
            var user = mis.MIS_USER_AP.Where(u => u.USER_ID == userID).SingleOrDefault();
            if (user != null)
            {
                userName = user.USER_NAME;
            }
        }
        return userName;
    }

    /// <summary>
    /// 由使用者編號取得部門編號
    /// </summary>
    /// <param name="userID">員工編號</param>
    /// <returns>部門編號</returns>
    public static string GetDeptIDByUserID(string userID)
    {
        string DeptID = string.Empty;
        using (MISEntities mis = new MISEntities())
        {
            var user = mis.MIS_USER_AP.Where(u => u.USER_ID == userID).SingleOrDefault();
            if (user != null)
            {
                DeptID = user.DEPT_ID;
            }
        }
        return DeptID;
    }
}